#include "Form_advancedStatistics.h"
#include "ui_Form_advancedStatistics.h"

Form_advancedStatistics::Form_advancedStatistics(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::Form_advancedStatistics)
{
	ui->setupUi(this);
}

Form_advancedStatistics::Form_advancedStatistics(QWidget *parent, QString group, bool detial) :
	QWidget(parent),
	ui(new Ui::Form_advancedStatistics)
{
	this->group = group;
	this->detial = detial;
	ui->setupUi(this);
	if (group == "all") {
		titleName = "所有的";
	}
	if (group == "year") {
		titleName = "今年的";
	}
	if (group == "month") {
		titleName = "这个月的";
	}
	if (group == "today") {
		titleName = "今天的";
	}
	if (detial)
		this->setWindowTitle(titleName+"细节");
	else
		this->setWindowTitle(titleName+"统计");

	chartView = new QChartView(this);
	chartView->setRenderHint(QPainter::Antialiasing);
	ui->scrollArea->setWidget(chartView);

	showBarChart();
}

Form_advancedStatistics::~Form_advancedStatistics()
{
	delete ui;
}

void Form_advancedStatistics::showLineChart()
{
	QLineSeries *series = new QLineSeries();

	series->append(0, 6);
	series->append(2, 4);
	series->append(3, 8);
	series->append(7, 4);
	series->append(10, 5);
	*series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);

	QChart *chart = new QChart();
	chart->legend()->hide();
	chart->addSeries(series);
	chart->createDefaultAxes();
	chart->setTitle("Simple line chart example");


	chartView->setChart(chart);
}

void Form_advancedStatistics::showBarChart()
{
	QList<record_t> records = dp.getMedicineRecordsByGroup(group, false);
	QBarSeries *series = new QBarSeries();
	series->setLabelsVisible(true);

//	QStringList categories;
//	categories << group;
//	//categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
//	for (int i=0; i<records.length(); i++) {
//		QBarSet* set0 = new QBarSet(records.at(i).name);
//		*set0 << records.at(i).sum;
//		series->append(set0);
//	}

	QStringList categories;
	QBarSet* set0 = new QBarSet("类别");
	//categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
	for (int i=0; i<records.length(); i++) {
		categories << records.at(i).name;
		*set0 << records.at(i).sum;

	}
	series->append(set0);

//	QBarSet *set0 = new QBarSet("Jane");
//	QBarSet *set1 = new QBarSet("John");
//	QBarSet *set2 = new QBarSet("Axel");
//	QBarSet *set3 = new QBarSet("Mary");
//	QBarSet *set4 = new QBarSet("Samantha");

//	*set0 << 1 << 2 << 3 << 4 << 5 << 6;
//	*set1 << 5 << 0 << 0 << 4 << 0 << 7;
//	*set2 << 3 << 5 << 8 << 13 << 8 << 5;
//	*set3 << 5 << 6 << 7 << 3 << 4 << 5;
//	*set4 << 9 << 7 << 5 << 3 << 1 << 2;



//	series->append(set0);
//	series->append(set1);
//	series->append(set2);
//	series->append(set3);
//	series->append(set4);


	QChart *chart = new QChart();
	chart->addSeries(series);
	chart->setTitle(titleName+" 的 数据统计");
	chart->setAnimationOptions(QChart::SeriesAnimations);






	QBarCategoryAxis *axis = new QBarCategoryAxis();
	axis->append(categories);
	chart->createDefaultAxes();
	chart->setAxisX(axis, series);


	chart->legend()->setVisible(true);
	chart->legend()->setAlignment(Qt::AlignBottom);


	chartView->setChart(chart);
}
